package org.xtense.model.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.dom4j.Node;

public class UpdateCache {
	// node == new added node created by us
	private HashMap<Node, String> newAddedPath = new HashMap<Node, String>();

	public UpdateCache() {

	}

	public void cachePath(Node us, String path) {
		newAddedPath.put(us, path);
	}
	
	public String getPath(Node us) {
		return newAddedPath.get(us);
	}
	
	public void removePath(Node us) {
		newAddedPath.remove(us);
	}

	// first one = created by user
	// second one = created by us
	private HashMap<Node, Node> newAddedNode = new HashMap<Node, Node>();
	
	public void cacheNode(Node user, Node us) {
		newAddedNode.put(user, us);
	}

	public Node getNode(Node user) {
		return newAddedNode.get(user);
	}
	
	public void removeNode(Node user) {
		newAddedNode.remove(user);
	}
	
	// node = us
	private HashMap<Node, int[]> newAddedPre = new HashMap<Node, int[]>();

	public void cachePre(Node us, int elementPre, int parentPre) {
		newAddedPre.put(us, new int[] { elementPre, parentPre });
	}

	public int[] getPre(Node us) {
		return newAddedPre.get(us);
	}

	public void removePre(Node us) {
		newAddedPre.remove(us);
	}
	public void updateForAdd(int elementPre, int size) {
		for (int[] pre : newAddedPre.values()) {
			if (pre[0] > elementPre) {
				pre[0] = pre[0] + size;
			}
			if (pre[1] > elementPre) {
				pre[1] = pre[1] + size;
			}
		}
	}
	public void updateForDel(int elementPre, int size) {
		for (int[] pre : newAddedPre.values()) {
			if (pre[0] > elementPre) {
				pre[0] = pre[0] - size;
			}
			if (pre[1] > elementPre) {
				pre[1] = pre[1] - size;
			}
		}
	}
	
	private List<String> sql = new ArrayList<String>();

	public void cacheSql(List<String> add) {
		sql.addAll(add);
	}
	public void cacheSql(String s) {
		sql.add(s);
	}
	public List<String> getSql() {
		return sql;
	}

}
